
package com.jf.cloud.platform.controller.platform;

import com.jf.cloud.common.database.dto.PageDTO;
import com.jf.cloud.common.database.vo.PageVO;
import com.jf.cloud.common.exception.LuckException;
import com.jf.cloud.common.response.ServerResponseEntity;
import com.jf.cloud.platform.dto.SysAccessKeyDTO;
import com.jf.cloud.platform.model.SysAccessKey;
import com.jf.cloud.platform.service.SysAccessKeyService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import ma.glasnost.orika.MapperFacade;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Objects;

/**
 * 系统密钥
 * @author zz
 */
@RestController("platformSysAccessKeyController")
@RequestMapping("/p/sys_access_key")
@Tag(name = "系统密钥")
public class SysAccessKeyController {

    @Autowired
    private SysAccessKeyService sysAccessKeyService;

    @Autowired
    private MapperFacade mapperFacade;

    @GetMapping("/page")
    @Operation(summary = "获取系统密钥列表", description = "分页获取系统密钥列表")
    public ServerResponseEntity<PageVO<SysAccessKey>> page(@Valid PageDTO pageDTO, @ParameterObject SysAccessKeyDTO sysAccessKeyDTO) {
        PageVO<SysAccessKey> sysAccessKeyPage = sysAccessKeyService.page(pageDTO, sysAccessKeyDTO);
        return ServerResponseEntity.success(sysAccessKeyPage);
    }

    @GetMapping
    @Operation(summary = "获取系统密钥", description = "根据sysAccessKeyId获取系统密钥")
    public ServerResponseEntity<SysAccessKey> getBySysAccessKeyId(@RequestParam Long sysAccessKeyId) {
        return ServerResponseEntity.success(sysAccessKeyService.getBySysAccessKeyId(sysAccessKeyId));
    }

    @PostMapping
    @Operation(summary = "保存系统密钥", description = "保存系统密钥")
    public ServerResponseEntity<Void> save(@Valid @RequestBody SysAccessKeyDTO sysAccessKeyDTO) {
        SysAccessKey sysAccessKey = mapperFacade.map(sysAccessKeyDTO, SysAccessKey.class);
        sysAccessKey.setSysAccessKeyId(null);
        sysAccessKeyService.save(sysAccessKey);
        return ServerResponseEntity.success();
    }

    @PutMapping
    @Operation(summary = "更新系统密钥", description = "更新系统密钥")
    public ServerResponseEntity<Void> update(@Valid @RequestBody SysAccessKeyDTO sysAccessKeyDTO) {
        SysAccessKey sysAccessKey = mapperFacade.map(sysAccessKeyDTO, SysAccessKey.class);
        sysAccessKeyService.update(sysAccessKey);
        sysAccessKeyService.removeCacheById(sysAccessKey.getAccessId());
        return ServerResponseEntity.success();
    }

    @PutMapping("/reset_access_key")
    @Operation(summary = "重置系统访问密钥" , description = "重置系统访问密钥")
    @Parameter(name = "sysAccessKeyId", description = "系统访问密钥id")
    public ServerResponseEntity<Boolean> resetAccessKey(@RequestParam("sysAccessKeyId") Long sysAccessKeyId) {
        if (Objects.isNull(sysAccessKeyId)) {
            throw new LuckException("sysAccessKeyId 不能为空");
        }
        SysAccessKey sysAccessKeyDb = sysAccessKeyService.getBySysAccessKeyId(sysAccessKeyId);
        if (Objects.isNull(sysAccessKeyDb)) {
            return ServerResponseEntity.success();
        }
        sysAccessKeyService.resetAccessKey(sysAccessKeyId);
        sysAccessKeyService.removeCacheById(sysAccessKeyDb.getAccessId());
        return ServerResponseEntity.success();
    }

    @DeleteMapping
    @Operation(summary = "删除系统密钥", description = "根据系统密钥id删除系统密钥")
    public ServerResponseEntity<Void> delete(@RequestParam Long sysAccessKeyId) {
        SysAccessKey sysAccessKey = sysAccessKeyService.getBySysAccessKeyId(sysAccessKeyId);
        if (Objects.isNull(sysAccessKey)) {
            return ServerResponseEntity.success();
        }
        sysAccessKeyService.deleteById(sysAccessKeyId);
        sysAccessKeyService.removeCacheById(sysAccessKey.getAccessId());
        return ServerResponseEntity.success();
    }
}
